package org.lx.nursing.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.lx.common.utils.DateUtils;
import org.lx.nursing.domain.NursingProject;
import org.lx.nursing.mapper.NursingProjectMapper;
import org.lx.nursing.service.INursingProjectService;
import org.lx.nursing.vo.NursingProjectVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class NursingProjectServiceImpl extends ServiceImpl<NursingProjectMapper, NursingProject> implements INursingProjectService {
    private static final String CACHE_KEY_PREFIX = "nursingProject:all";

    @Autowired
    private NursingProjectMapper nursingProjectMapper;

    @Autowired
    private RedisTemplate<Object, Object> redisTemplate;

    @Override
    public NursingProject selectNursingProjectById(Long id) {
        return nursingProjectMapper.selectById(id);
    }

    @Override
    public List<NursingProject> selectNursingProjectList(NursingProject nursingProject) {
        return nursingProjectMapper.selectNursingProjectList(nursingProject);
    }

    @Override
    public int insertNursingProject(NursingProject nursingProject) {
        nursingProject.setCreateTime(DateUtils.getNowDate());
        int i = nursingProjectMapper.insert(nursingProject);
        redisTemplate.delete(CACHE_KEY_PREFIX);
        return i;
    }

    @Override
    public int updateNursingProject(NursingProject nursingProject) {
        nursingProject.setUpdateTime(DateUtils.getNowDate());
        int i = nursingProjectMapper.updateById(nursingProject);
        redisTemplate.delete(CACHE_KEY_PREFIX);
        return i;
    }

    @Override
    public int deleteNursingProjectByIds(Long[] ids) {
        int i = nursingProjectMapper.deleteNursingProjectByIds(ids);
        redisTemplate.delete(CACHE_KEY_PREFIX);
        return i;
    }

    @Override
    public int deleteNursingProjectById(Long id) {
        return nursingProjectMapper.deleteById(id);
    }

    @Override
    @SuppressWarnings("unchecked")
    public List<NursingProjectVo> getAllProjects() {
        List<NursingProjectVo> list = (List<NursingProjectVo>) redisTemplate.opsForValue().get(CACHE_KEY_PREFIX);
        if (list != null && !list.isEmpty()) return list;
        list = nursingProjectMapper.getAllProjects();
        redisTemplate.opsForValue().set(CACHE_KEY_PREFIX, list);
        return list;
    }
}
